package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbOrderItemMapper;
import com.dongdongshop.mapper.TbOrderMapper;
import com.dongdongshop.pojo.TbAddress;
import com.dongdongshop.pojo.TbOrder;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.OrderService;
import com.dongdongshop.vo.CartVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@DubboService
public class OrderServiceImpl implements OrderService {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private TbOrderMapper tbOrderMapper;
    @Resource
    private TbOrderItemMapper tbOrderItemMapper;
    @DubboReference
    private CartService cartService;
    @DubboReference
    private AddressService addressService;

    @Override
    public void saveInfo(Long addrId, String payType, TbUser user) {
        List<CartVO> cartList = cartService.selectCartListFromRedis(user.getId());
        String str = "";
        for (CartVO cart : cartList) {
            IdWorker idWorker = new IdWorker();
            long nextId = idWorker.nextId();//设置id
            System.out.println("sellerId"+cart.getSellerId()    );
            TbOrder order = new TbOrder();
            order.setOrderId(nextId);//订单id
            order.setUserId(user.getUsername());//用户名
            order.setPaymentType("1");//支付方式
            order.setStatus("1");//状态未付款
            order.setCreateTime(new Date());
            order.setUpdateTime(new Date());
            TbAddress address = addressService.selectById(addrId);
            order.setReceiverAreaName(address.getAddress());//地址
            order.setReceiverMobile(address.getMobile());//电话
            order.setReceiver(address.getContact());//收货人
            order.setSellerId(cart.getSellerId());//商家id

            IdWorker idWorker1 = new IdWorker();
            long orderNum = idWorker1.nextId();//增加订单号
            str = Long.toString(orderNum);
            order.setOrderNum(str);

            double money = 0;
            for (TbOrderItem orderItem : cart.getOrderItemList()) {
                orderItem.setOrderNum(str);//增加订单号
                orderItem.setId(idWorker.nextId());
                orderItem.setOrderId(nextId);//订单id
                orderItem.setSellerId(cart.getSellerId());
                money += orderItem.getTotalFee().doubleValue();
                tbOrderItemMapper.insertSelective(orderItem);//增加从表
            }
            order.setPayment(new BigDecimal(money));
            tbOrderMapper.insertSelective(order);//增加主表
        }
        redisTemplate.boundHashOps("CartList").delete(user.getId());
    }
}
